import re

from bs4 import BeautifulSoup

from util import utils

videoURL = 'http://94.91p31.space/view_video.php?viewkey=4aff2c35a6b7509fdaaf&page=3&viewtype=basic&category=mr'
listUrl = "http://94.91p31.space/video.php?category=rf&page="
path = "./video/"


def getAllImg(html):
    imgre = re.compile(r'file=\"(.+?\.jpg)\"')
    imglist = imgre.findall(html)
    return imglist


def getVideoTag(html):
    t = BeautifulSoup(html, 'html.parser').find_all('video')
    print(t.__len__())
    if t.__len__() == 0:
        return
    imgre = re.compile(r'\<source src=\"(.+?)\?s')
    url = imgre.findall(str(t))[0]
    print(url)

    return url


def downLoadVideo(url,path,fileName):
    utils.mkdir(path)
    utils.downLoad(url, fileName)


def getVideoURLFromHtml(videoHtml):
    print (utils.getHtmlUtf_8(videoURL))


def getUrlListByPage(pageHtml):
    urllist = []
    t = BeautifulSoup(pageHtml, 'html.parser').find_all('a')
    for t1 in t:
        s = str(t1.get('href'))
        if s.__contains__("http://94.91p31.space/view_video.php?viewkey"):
            urllist.append(s)
    return urllist


def run(index):
    everyPage = getUrlListByPage(utils.getHtmlUtf_8(listUrl + str(index)))
    for url in everyPage[0:5]:
        tag = getVideoTag(utils.getHtmlUtf_8(url))
        print(tag)
        downLoadVideo(tag, path, path + utils.getTimeName() + ".mp4")


if __name__ == "__main__":
    run(1)

